package com.qiantu.phone.ui.fragment;

import static com.qiantu.phone.ui.fragment.device.BaseDeviceFragment.STATUS;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.lib.migu.MiguManager;
import com.qiantu.api.db.LLMusicDBManager;
import com.qiantu.api.entity.MusicDaoBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppFragment;
import com.rich.czlylibary.bean.SongNewVoiceBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * @Description: 搜索音乐列表
 * @Time:  2023年1月30日
 */
public class MusicTextFragment extends AppFragment<AppActivity>{
    private String serialNo;
    private int count;  //当前播放歌曲下标
    private RecyclerView music_recyclerview;
    private MusiclistAdapter musiclistAdapter;
    private String music_name;
    private boolean ifscene = false;
    private String dialog_sign;
    private List<MusicDaoBean> getmusicbean = new ArrayList<>();
    private List<SongNewVoiceBox> mSongNewVoiceBoxes = new ArrayList<>();
    private CheckBox checkbox_music;
    private LinearLayout ll_top_name;



    //回调接口
    public interface MusicTextInterface{
        void sendValue_text(String musicid,String musicName, String songpicurl);
    }

    private MusicTextInterface musicTextInterface;



    public static MusicTextFragment newInstance(String serialNo,String music_name, String dialog_sign) {
        MusicTextFragment fragment = new MusicTextFragment();
        Bundle bundle = new Bundle();
        bundle.putString("serialNo",serialNo);
        bundle.putString("music_name",music_name);
        bundle.putString("dialog_sign",dialog_sign);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        if (arguments!=null){
            serialNo = arguments.getString("serialNo");
            music_name = arguments.getString("music_name");
            dialog_sign = arguments.getString("dialog_sign");
        }
        musicTextInterface = (MusicTextInterface) getActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_music_text;
    }

    @Override
    protected void initView() {
        music_recyclerview = findViewById(R.id.music_Recyclerview);
        ll_top_name = findViewById(R.id.ll_top_name);
        ifscene = dialog_sign.equals("deviceDialog");
        MiguManager instance = MiguManager.getInstance();
        instance.searchSong(music_name, 1, 20, new MiguManager.MiguCallback<List<SongNewVoiceBox>>() {
            @Override
            public void callback(List<SongNewVoiceBox> songNewVoiceBoxes) {
                mSongNewVoiceBoxes = songNewVoiceBoxes;
                ll_top_name.setVisibility(View.VISIBLE);
                initAdapter();
            }
        }, new MiguManager.MiguCallback<String>() {
            @Override
            public void callback(String s) {
                String s1 = s.toString();
            }
        });

    }


    @Override
    protected void initData() {

    }

    private void initAdapter() {
        musiclistAdapter = new MusiclistAdapter(mSongNewVoiceBoxes);
        music_recyclerview.setAdapter(musiclistAdapter);
        music_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        if (!ifscene){
            musiclistAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    getmusicbean = LLMusicDBManager.getInstance(getContext()).getmusicbean();
                    MusicDaoBean musicDaoBean = null;
                    if (mSongNewVoiceBoxes.get(position).getComposer() != null && mSongNewVoiceBoxes.get(position).getComposer().size()!=0){
                        musicDaoBean = new MusicDaoBean(
                                null,
                                mSongNewVoiceBoxes.get(position).getName(),
                                mSongNewVoiceBoxes.get(position).getComposer().get(0),
                                false,
                                "migu"+mSongNewVoiceBoxes.get(position).getFullSongs()[0].getCopyrightId());

                        LLMusicDBManager.getInstance(getContext()).putbean(musicDaoBean);
                    }
                    SwitchMusicOperation("migu"+mSongNewVoiceBoxes.get(position).getFullSongs()[0].getCopyrightId());
                }
            });
        }
    }




    private class MusiclistAdapter extends BaseQuickAdapter<SongNewVoiceBox, BaseViewHolder>{

        public MusiclistAdapter(@Nullable List<SongNewVoiceBox> data) {
            super(R.layout.item_music_list, data);
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            holder.setText(R.id.music_id,position+1+"、");
            TextView music_name = holder.getView(R.id.music_name);
            TextView singer_name = holder.getView(R.id.singer_name);
            music_name.setTextColor(R.attr.themeColor);
            singer_name.setTextColor(R.attr.themeColor);

            if (position == count){
                checkbox_music.setChecked(true);
                musicTextInterface.sendValue_text(mSongNewVoiceBoxes.get(position).getFullSongs()[0].getCopyrightId(),mSongNewVoiceBoxes.get(position).getName(),"");
            }else {
                checkbox_music.setChecked(false);
            }
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, SongNewVoiceBox item) {
            holder.setText(R.id.music_name,item.getName());
            if (item.getComposer()!=null){
                holder.setText(R.id.singer_name,item.getComposer().get(0));
            }else {
                holder.setText(R.id.singer_name,"");
            }
            ImageView music_like = holder.getView(R.id.music_like);
            ImageView music_details = holder.getView(R.id.music_details);
            checkbox_music = holder.getView(R.id.checkbox_music);
            music_like.setVisibility(View.GONE);

            if(ifscene){
                music_like.setVisibility(View.GONE);
                music_details.setVisibility(View.GONE);
                checkbox_music.setVisibility(View.VISIBLE);
                checkbox_music.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        count = holder.getPosition();
                        musicTextInterface.sendValue_text(item.getFullSongs()[0].getCopyrightId(),item.getName(),"");
                        notifyDataSetChanged();
                    }
                });
            }
        }
    }


    private void SwitchMusicOperation(String musicID) {
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> statusMap=new HashMap<>();
        map.put("songid",musicID);
        statusMap.put(STATUS,map);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(),serialNo,statusMap,new HttpCallback<HttpData<Void>>(null){
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                finish();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }
}